package ms.pms.dao;

import mapper.MsMapper;
import ms.pms.entity.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface PermissionDAO extends MsMapper<Permission> {

    /**
     * 分页查询
     * @param status
     * @return
     */
    @Select("<script>"
            + "select * from t_permission where 1=1 "
            + "<if test=\"status != 0 \"> and status=#{status} </if> "
            + "<if test=\"name != null and name != '' \"> and name like '%${name}%' </if> "
            + "order by create_time desc" + "</script>")
    @ResultType(Permission.class)
    List<Permission> query(@Param("status") Integer status, @Param("name") String name);

    /**
     * @param permissionIds
     * @return
     */
    @Select("<script>"
            + "select * from t_permission where 1=1 "
            + "<if test=\"permissionIds != null  \"> and id in " +
            "<foreach collection=\"permissionIds\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">\n" +
            "  #{item}"
            + "</foreach> "
            + " </if> "
            + "order by create_time desc" + "</script>")
    @ResultType(Permission.class)
    List<Permission> selectPermissionsByIds(@Param("permissionIds") List<Long> permissionIds);


    @Select("select * from t_permission where id in (select permission_id from t_role_permission where role_id=#{roleId}) ")
    @ResultType(Permission.class)
    List<Permission> getPermissionsByRoleId(@Param("roleId") Long roleId);
}
